package org.ax.config;

import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.ax.interceptor.LoginInterceptor;
import org.ax.interceptor.UTF8Interceptor;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.servlet.MultipartConfigElement;

/**
 * 编程人:赵浩鑫
 * 时间:2021-01-29-11:12
 */
@Configuration
@EnableWebMvc
@ComponentScan({"org.ax.controller","org.ax.acrSoft.controller"})
public class WebMvc implements WebMvcConfigurer {

    @Bean
    public CommonsMultipartResolver multipartResolver(){
        CommonsMultipartResolver c=new CommonsMultipartResolver();
        c.setDefaultEncoding("utf-8");
        c.setMaxUploadSize(20971520);
//        c.setDefaultEncoding("<meta http-equiv=\"Content-Type\" content=\" charset=utf-8\" />");
        return c;
    }

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();

        factory.setMaxFileSize(DataSize.ofBytes(1073741824L));
        factory.setMaxRequestSize(DataSize.ofBytes(1073741824L));
        return factory.createMultipartConfig();
    }
    @Override //支持跨域访问
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
                .allowCredentials(true)
                .allowedHeaders("*")
                .maxAge(3600);
    }
    @Bean
    public SpringResourceTemplateResolver springResourceTemplateResolver(){
        SpringResourceTemplateResolver s=new SpringResourceTemplateResolver();
        s.setPrefix("classpath:templates/");
        s.setSuffix(".html");
        s.setCharacterEncoding("UTF-8");
        s.setOrder(1);
        s.setTemplateMode("HTML5");
        s.setCacheable(false);
        return s;
    }

    @Bean
    public SpringTemplateEngine templateEngine(){
        SpringTemplateEngine s=new SpringTemplateEngine();
        s.setTemplateResolver(springResourceTemplateResolver());
        return s;
    }
    @Bean
    public ThymeleafViewResolver thymeleafViewResolver(){
        ThymeleafViewResolver t=new ThymeleafViewResolver();
        t.setTemplateEngine(templateEngine());
        t.setCharacterEncoding("UTF-8");
        return t;
    }

//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(new UTF8Interceptor())
//                .addPathPatterns("/**");
//    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/layuiadmin/**").
                addResourceLocations("classpath:/layuiadmin/");
        registry.addResourceHandler("/websocket/**").
                addResourceLocations("classpath:/websocket/");
        registry.addResourceHandler("/webjars/**").
                addResourceLocations("/webjars/");
        registry.addResourceHandler("/images/**").
                addResourceLocations("file:F:\\Y2data\\SSM\\SSM-production\\src\\main\\resources\\img\\");
    }
    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){
        DefaultAdvisorAutoProxyCreator d=new DefaultAdvisorAutoProxyCreator();
        d.setProxyTargetClass(true);
        return d;
    }
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager defaultWebSecurityManager){
        AuthorizationAttributeSourceAdvisor a=new AuthorizationAttributeSourceAdvisor();
        a.setSecurityManager(defaultWebSecurityManager);
        return a;
    }
}
